package com.efast.dmom.equipment.service.maintain.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.equipment.bean.EquipMaintainItemParsmBean;
import com.efast.dmom.equipment.entity.EquipCheckingItems;
import com.efast.dmom.equipment.entity.EquipMaintainItems;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.equipment.dao.maintain.EquipMaintainDao;
import com.efast.dmom.equipment.service.maintain.IEquipMaintainService;

@Service
public class EquipMaintainServiceImpl implements IEquipMaintainService {
	
	@Autowired
	private EquipMaintainDao equipMaintainDao;

	@Override
	public void deleteMaintainRules(List<Integer> ids) {
		if (ids.size() > 0 && ids != null) {
		equipMaintainDao.deleteMaintainRules(ids);
		}
	}

	@Override
	public void saveMaintainOrderItems(EquipMaintainItemParsmBean bean) {
		// 查询需要保存的保养规则
		List<EquipMaintainItems> maintainItems = equipMaintainDao
				.queryMaintainOrderItemsInfoByRuleIds(bean.getEquipMaintainItems());
		if (maintainItems != null && maintainItems.size() > 0) {

			Map<Integer, EquipMaintainItems> map = new HashMap<>();

			for (EquipMaintainItems equipMaintainItems : bean.getEquipMaintainItems()) {
				map.put(equipMaintainItems.getRuleId(), equipMaintainItems);
			}

			for (EquipMaintainItems item : maintainItems) {
				item.setSpareCount(map.get(item.getRuleId()).getSpareCount());
				item.setSpareName(map.get(item.getRuleId()).getSpareName());
				item.setSpareUnit(map.get(item.getRuleId()).getSpareUnit());
				item.setAdjustedValue(map.get(item.getRuleId()).getAdjustedValue());
				item.setOrderId(bean.getOrderId());
				item.setCreateTime(new Date());
				item.setCreateUser(SpringWebUtils.getRequestUser());
				item.setLastUpdateTime(new Date());
				item.setLastUpdateUser(SpringWebUtils.getRequestUser());
				item.setCompanyCode(SpringWebUtils.getRequestCompany());
			}
			// 保存保养项
			equipMaintainDao.saveMaintainOrderItems(maintainItems);
		}
	}

	@Override
	public void updateMaintainOrderItems(EquipMaintainItemParsmBean bean) {
		for(EquipMaintainItems e:bean.getEquipMaintainItems()){
			e.setMaintainer(SpringWebUtils.getRequestUser());
		}
		equipMaintainDao.updateMaintainOrderItems(bean.getEquipMaintainItems());
	}

	@Override
	public void editMaintainOrderItems(EquipMaintainItemParsmBean bean) {
		//删除已有保养项
		equipMaintainDao.deleteMaintainOrderItems(bean.getOrderId());
		//新增保养项
		this.saveMaintainOrderItems(bean);
	}
}
